/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Br.Com.Cds.Academia.Banco;

import Br.Com.Cds.Academia.DAO.AcessoMySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Pedro
 */
public class BancoControl {
    PreparedStatement pstm;
    ResultSet rs;
    
    
    String consultabanco = "SELECT * FROM BANCO WHERE BANCO LIKE ?";
    String cadastrabanco = "INSERT INTO BANCO (BANCO,AGENCIA,CONTA,STATUS)VALUES (?,?,?,?)";
    String alterarbanco = "UPDATE BANCO SET BANCO=?,AGENCIA=?,CONTA=?, STATUS=? WHERE IDBANCO=?";
    String excluirbanco = "DELETE FROM BANCO WHERE IDBANCO=?"; 
    
    
    
    public void cadastraBanco (BancoBean banco){
        try {
            AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(cadastrabanco);
            pstm.setString(1,banco.getBanco());
            pstm.setString(2, banco.getAgencia());
            pstm.setString(3, banco.getConta());
            pstm.setString(4, banco.getStatus());
            pstm.executeUpdate();
            mysql.desconectar();
    
        } catch (SQLException e) {
            e.printStackTrace ();
            JOptionPane.showMessageDialog(null, "SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UM BANCO!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE); 
        }
}


public void alterarBanco (BancoBean banco){
    
    try {
        AcessoMySQL mysql = new AcessoMySQL();
        pstm = (PreparedStatement) mysql.conectar().prepareStatement(alterarbanco);
        pstm.setString(1, banco.getBanco());
        pstm.setString(2,banco.getAgencia());
        pstm.setString(3, banco.getConta());
        pstm.setString(4, banco.getStatus());
        pstm.setInt(5, banco.getIdbanco());
        pstm.executeUpdate();
        mysql.desconectar(); 
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,"SQL EXCEPTION ! ERRO AO TENTAR ALTERAR UM BANCO!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
    }
}

    
  public List <BancoBean> listarBanco (String nomeBanco){
         List <BancoBean> bancos = new ArrayList ();
         
         try {
             AcessoMySQL mysql = new AcessoMySQL();
             pstm = (PreparedStatement) mysql.conectar().prepareStatement(consultabanco);
             pstm.setString(1, nomeBanco);
             rs = pstm.executeQuery();
             BancoBean alu;
             
             while (rs.next()){
                alu = new BancoBean ();
                alu.setIdbanco(rs.getInt("IDBANCO"));          
                alu.setBanco(rs.getString("BANCO"));  
                alu.setAgencia(rs.getString("AGENCIA")); 
                alu.setConta(rs.getString("CONTA")); 
                alu.setStatus(rs.getString("STATUS"));          
                bancos.add(alu);  
             }    
      } catch (Exception e) {
        e.printStackTrace();
         JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR REALIZAR UMA CONSULTA","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
        
      }
        return bancos;
      
  }
    
    
  public void excluirBanco (int idbanco){
      
      try {
          AcessoMySQL mysql = new AcessoMySQL();
          pstm = (PreparedStatement) mysql.conectar().prepareStatement(excluirbanco);
          pstm.setInt(1,idbanco);
          pstm.executeUpdate();
          mysql.desconectar();
            
      } catch (Exception e) {
          e.printStackTrace();
          JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR EXCLUIR O BANCO","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
      }
      
      
      
      
  }

    
    
    
    
    
    
    
    
    

}